package com.ruoyi.project.projectInfo.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.project.projectUsers.domain.ProjectUsers;
import com.ruoyi.project.projectUsers.mapper.ProjectUsersMapper;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.projectInfo.mapper.ProjectInfoMapper;
import com.ruoyi.project.projectInfo.domain.ProjectInfo;
import com.ruoyi.project.projectInfo.service.IProjectInfoService;
import com.ruoyi.common.core.text.Convert;
import oshi.util.StringUtil;

/**
 * 项目信息Service业务层处理
 *
 * @author ruoyi
 * @date 2020-05-28
 */
@Service
public class ProjectInfoServiceImpl implements IProjectInfoService
{
    @Autowired
    private ProjectInfoMapper projectInfoMapper;
  @Autowired
  private SysUserMapper sysUserMapper;
  @Autowired
  private ProjectUsersMapper projectUsersMapper;

    /**
     * 查询项目信息
     *
     * @param id 项目信息ID
     * @return 项目信息
     */
    @Override
    public ProjectInfo selectProjectInfoById(Long id)
    {
        return projectInfoMapper.selectProjectInfoById(id);
    }

    /**
     * 查询项目信息列表
     *
     * @param projectInfo 项目信息
     * @return 项目信息
     */
    @Override
    public List<ProjectInfo> selectProjectInfoList(ProjectInfo projectInfo)
    {
        return projectInfoMapper.selectProjectInfoList(projectInfo);
    }

    /**
     * 新增项目信息
     *
     * @param projectInfo 项目信息
     * @return 结果
     */
    @Override
    public int insertProjectInfo(ProjectInfo projectInfo)
    {
        return projectInfoMapper.insertProjectInfo(projectInfo);
    }

    /**
     * 修改项目信息
     *
     * @param projectInfo 项目信息
     * @return 结果
     */
    @Override
    public int updateProjectInfo(ProjectInfo projectInfo)
    {
        return projectInfoMapper.updateProjectInfo(projectInfo);
    }

    /**
     * 删除项目信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProjectInfoByIds(String ids)
    {
        return projectInfoMapper.deleteProjectInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目信息信息
     *
     * @param id 项目信息ID
     * @return 结果
     */
    @Override
    public int deleteProjectInfoById(Long id)
    {
        return projectInfoMapper.deleteProjectInfoById(id);
    }

  @Override
  public int importUsers(Long projectId, Long[] ids) {
    ProjectInfo projectInfo = projectInfoMapper.selectProjectInfoById(projectId);
    List<SysUser> sysUsers = sysUserMapper.selectUsersByIds(ids);
    List<ProjectUsers> projectUsersList = new ArrayList<>();
    for (SysUser sysUser : sysUsers) {
      ProjectUsers projectUsers = new ProjectUsers();
      projectUsers.setUserid(sysUser.getUserId());
      projectUsers.setUserName(sysUser.getUserName());
      projectUsers.setPhonenumber(sysUser.getPhonenumber());
      projectUsers.setLoginName(sysUser.getLoginName());
      List<SysPost> posts = sysUser.getPosts();
      if (posts != null && posts.size() >= 1) {
        StringBuilder sb = new StringBuilder();
        for (SysPost sysPost : posts) {
          String postName = sysPost.getPostName();
          if (!StringUtils.isEmpty(postName))
            sb.append(postName).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        projectUsers.setPostsdesc(sb.toString());
      }
      if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
        projectUsers.setCreateBy(ShiroUtils.getLoginName());
      }

      projectUsers.setProjectinfoid(projectInfo.getId());
      projectUsers.setProjectinfoname(projectInfo.getName());
      projectUsersList.add(projectUsers);
    }
    if (projectUsersList.size() > 0) {
      return projectUsersMapper.batchUserPost(projectUsersList);
    }

    return 0;
  }

  public static void main(String[] args) {
    String arr[] = {"str1", "str2"};
    StringBuilder sb = new StringBuilder();
    for (String str1 : arr) {
      sb.append(str1).append(",");
    }
    sb.deleteCharAt(sb.length() - 1);
    System.out.println(sb);
  }
}
